<?php
include_once("Constants.php");
include_once("Entity/facebook_app.php");

class facebook_app_dao_base {

	function delete_facebook_app($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("DELETE FROM facebook_app WHERE app_id = :app_id");
			$stmt->bindParam(':app_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function get_facebook_app($id)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();
			
			$stmt = $dbh->prepare("SELECT * FROM  facebook_app f WHERE f.app_id = :app_id");
			$stmt->bindParam(':app_id', $id, PDO::PARAM_INT);
			$stmt->execute();
			
			$f = null;
			if($row = $stmt->fetch())
			{
				$f = new facebook_app($row["app_id"],$row["app_api_key"],$row["app_secret"],$row["app_name"],$row["url_canvas"]);
			}
			
			$dbh->commit();
			$dbh = null;
			return $f;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}
	
	function get_facebook_app_list()
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$aof = array();
			$stmt = $dbh->query("SELECT * FROM facebook_app");
			$rows = $stmt->fetchAll();
			foreach($rows as $row)
			{
				$f = new facebook_app($row["app_id"],$row["app_api_key"],$row["app_secret"],$row["app_name"],$row["url_canvas"]);
				$aof[] = $f;
			}

			$dbh->commit();
			$dbh = null;
			
			return $aof;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function insert_facebook_app($f)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("INSERT INTO facebook_app (app_api_key, app_secret, app_name, url_canvas)
							VALUE(:app_api_key ,:app_secret ,:app_name ,:url_canvas)");
							
							
			$stmt->bindParam(':app_api_key', $f->app_api_key);
			$stmt->bindParam(':app_secret', $f->app_secret);
			$stmt->bindParam(':app_name', $f->app_name);
			$stmt->bindParam(':url_canvas', $f->url_canvas);
			
			$stmt->execute();
			$id = $dbh->lastInsertId();	
			$dbh->commit();
			$dbh = null;

			return $id;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

	function update_facebook_app($f)
	{
		try
		{
			$dbh = new PDO(Constants::$connectionString, Constants::$username, Constants::$password);
			$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$dbh->beginTransaction();

			$stmt = $dbh->prepare("UPDATE facebook_app f 
						SET 
						f.app_api_key = :app_api_key,
						f.app_secret = :app_secret,
						f.app_name = :app_name,
						f.url_canvas = :url_canvas
						WHERE 
						f.app_id = :app_id");
			$stmt->bindParam(':app_api_key', $f->app_api_key);
			$stmt->bindParam(':app_secret', $f->app_secret);
			$stmt->bindParam(':app_name', $f->app_name);
			$stmt->bindParam(':url_canvas', $f->url_canvas);
			$stmt->bindParam(':app_id', $f->app_id);
			
			$stmt->execute();
			
			$dbh->commit();
			$dbh = null;
		}
		catch(PDOException $e)
		{
			echo '<br />' . $e->getMessage();
			$dbh->rollback();
		}	
	}

}
?>